﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Editor.Utilities;
using Editor.PageControls;

namespace Editor
{
    public partial class ImageBoxProperties : PropertiesFormBase, IDictSubject<byte[]>
    {
        private bool scrollableImage
        {
            set
            {
                textBoxWidthS.Enabled = textBoxHeightS.Enabled = textBoxScroll.Enabled = value;
            }
        }

        public ImageBoxProperties()
        {
            InitializeComponent();
        }

        private void buttonImage_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                panelImage.BackgroundImage = Image.FromStream(openFileDialog1.OpenFile());
        }

        protected override void buttonApply_Click(object sender, EventArgs e)
        {
            PageImageBox selectedImageBox = PageControl.SelectedControl as PageImageBox;

            if (textBoxName.Text.Trim() == string.Empty)
            {
                MessageBox.Show("İsim girmelisiniz!", "Hata!", MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
                DialogResult = System.Windows.Forms.DialogResult.Retry;
                return;
            }

            if (!selectedImageBox.Name.Equals(textBoxName.Text.Trim()) &&
                !EditorForm.IsElementNameAvailable(textBoxName.Text.Trim()))
            {
                MessageBox.Show("Bu isim zaten var!", "Hata!", MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
                DialogResult = System.Windows.Forms.DialogResult.Retry;
                return;
            }

            if (panelImage.BackgroundImage == null)
            {
                MessageBox.Show("Resim seçmelisiniz!", "Hata!", MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
                DialogResult = System.Windows.Forms.DialogResult.Retry;
                return;
            }

            int width = -1, height = -1;

            if (!int.TryParse(textBoxWidth.Text, out width) || width <= 0)
            {
                MessageBox.Show("Genişlik değeri pozitif bir tam sayı olmalı!",
                    "Hata!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                DialogResult = System.Windows.Forms.DialogResult.Retry;
                return;
            }

            if (!int.TryParse(textBoxHeight.Text, out height) || height <= 0)
            {
                MessageBox.Show("Yükseklik değeri pozitif bir tam sayı olmalı!",
                    "Hata!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                DialogResult = System.Windows.Forms.DialogResult.Retry;
                return;
            }

            int x = -1, y = 1;

            if (!int.TryParse(textBoxX.Text, out x) || x < 0)
            {
                MessageBox.Show("X değeri pozitif bir tam sayı olmalı!", "Hata!",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                DialogResult = System.Windows.Forms.DialogResult.Retry;
                return;
            }

            if (!int.TryParse(textBoxY.Text, out y) || y < 0)
            {
                MessageBox.Show("Y değeri pozitif bir tam sayı olmalı!", "Hata!",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                DialogResult = System.Windows.Forms.DialogResult.Retry;
                return;
            }

            int swidth = -1, sheight = -1;
            int scroll = -1;

            if (checkBoxIsLong.Checked)
            {
                if (!int.TryParse(textBoxWidthS.Text, out swidth) || swidth <= 0)
                {
                    MessageBox.Show("Küçültülen genişlik değeri pozitif bir tam sayı olmalı!",
                        "Hata!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    DialogResult = System.Windows.Forms.DialogResult.Retry;
                    return;
                }

                if (!int.TryParse(textBoxHeightS.Text, out sheight) || sheight <= 0)
                {
                    MessageBox.Show("Küçültülen yükseklik değeri pozitif bir tam sayı olmalı!",
                        "Hata!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    DialogResult = System.Windows.Forms.DialogResult.Retry;
                    return;
                }

                if (!int.TryParse(textBoxScroll.Text, out scroll) || scroll <= 0)
                {
                    MessageBox.Show("Kaydırma değeri pozitif bir tam sayı olmalı!",
                        "Hata!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    DialogResult = System.Windows.Forms.DialogResult.Retry;
                    return;
                }
            }
            else
            {
                swidth = width;
                sheight = height;
                scroll = 0;
            }

            string selectedLanguage = comboBoxLanguage.SelectedItem as string;

            selectedImageBox.Name = textBoxName.Text.Trim();
            selectedImageBox.Location = new Point(x, y);
            Dictionary[selectedLanguage] = ImageWorker.GetByteArray(panelImage.BackgroundImage);
            selectedImageBox.Dictionary = Dictionary;
            //selectedImageBox.Image = panelImage.BackgroundImage;
            selectedImageBox.Size = new Size(width, height);
            selectedImageBox.IsLong = checkBoxIsLong.Checked;
            selectedImageBox.IsFormula = checkBoxIsFormula.Checked;
            selectedImageBox.ScrollSize = scroll;

            if (selectedImageBox.IsLong)
                selectedImageBox.ScrollableSize = new Size(swidth, sheight);
            else
                selectedImageBox.ScrollableSize = selectedImageBox.Size;

            base.buttonApply_Click(sender, e);
        }

        protected override void PropertiesForm_Load(object sender, EventArgs e)
        {
            base.PropertiesForm_Load(sender, e);

            PageImageBox selectedImageBox = PageControl.SelectedControl as PageImageBox;

            ControlName = selectedImageBox.Name;
            textBoxName.Text = selectedImageBox.Name;
            textBoxX.Text = selectedImageBox.Location.X.ToString();
            textBoxY.Text = selectedImageBox.Location.Y.ToString();
            textBoxWidth.Text = selectedImageBox.Size.Width.ToString();
            textBoxHeight.Text = selectedImageBox.Size.Height.ToString();
            checkBoxIsFormula.Checked = selectedImageBox.IsFormula;
            checkBoxIsLong.Checked = selectedImageBox.IsLong;

            if (selectedImageBox.ScrollSize <= 0)
                selectedImageBox.ScrollSize = 20;

            textBoxScroll.Text = selectedImageBox.ScrollSize.ToString();

            Dictionary = selectedImageBox.Dictionary;
            if (comboBoxLanguage.SelectedIndex == -1)
                comboBoxLanguage.Items.AddRange(EditorForm.SupportedLanguages);
            panelImage.BackgroundImage =
                ImageWorker.GetImage(selectedImageBox.Dictionary[EditorForm.CurrentLanguage]);
            resetSubject();
            comboBoxLanguage.SelectedIndex = EditorForm.CurrentLanguageIndex;

            if (selectedImageBox.IsLong)
            {
                textBoxWidthS.Text = selectedImageBox.ScrollableSize.Width.ToString();
                textBoxHeightS.Text = selectedImageBox.ScrollableSize.Height.ToString();
            }
            else
            {
                textBoxWidthS.Text = selectedImageBox.Size.Width.ToString();
                textBoxHeightS.Text = selectedImageBox.Size.Height.ToString();
                textBoxWidthS.Enabled = textBoxHeightS.Enabled = textBoxScroll.Enabled = false;
            }
        }

        protected override void buttonRemove_Click(object sender, EventArgs e)
        {
            base.buttonRemove_Click(sender, e);
        }

        protected override void buttonCopy_Click(object sender, EventArgs e)
        {
            base.buttonCopy_Click(sender, e);
        }

        protected override void buttonRelocate_Click(object sender, EventArgs e)
        {
            base.buttonRelocate_Click(sender, e);
        }

        private void checkBoxIsLong_CheckedChanged(object sender, EventArgs e)
        {
            scrollableImage = checkBoxIsLong.Checked;
        }

        public SDictionary<byte[]> Dictionary { get; set; }

        private void comboBoxLanguage_SelectedIndexChanged(object sender, EventArgs e)
        {
            SDictionary<byte[]> dict = Dictionary;

            panelImage.BackgroundImage = ImageWorker.GetImage(
                changeSubject(comboBoxLanguage.SelectedItem as string,
                    comboBoxLanguage.SelectedIndex,
                    ImageWorker.GetByteArray(panelImage.BackgroundImage),
                    ref dict));
            Dictionary = dict;
        }
    }
}
